import React,{Component} from 'react';

var username = 'test';
var password = 'test';

export default class NetUtils extends Component{
	constructor(props){
		super(props);
	}

    static genData(service,opts)
    {
        switch (service) {
        case 'register':
            username = opts.username;
            password = opts.password;
            return(
                'message-type=register'+'&username='+username+'&password='+password
            )
            break;

        case 'login':
            username = opts.username;
            password = opts.password;
            return(
                'message-type=login'+'&username='+username+'&password='+password
            )
            break;

        case 'new_day':
            return(
                'message-type=new_day'+'&username='+username
            )
            break;


        /*打卡
        var service = 'new_card';
        var url = 'http://172.18.159.243:8000/api';
        var opts ={
            taskId:7,
            finishedCard:true
        };
        */
        case 'new_card':
             var taskId = opts.taskId;
             var day = this.getCurrentDateFormat;
             //var day = '2018-01-01'
             var finishedCard = opts.finishedCard;
            /* console.log(
                'message-type=new_card'+'&username='+username
                +'&taskId='+taskId
                +'&day='+day
                +'&finishedCard='+finishedCard
            );*/
             return(
                'message-type=new_card'+'&username='+username
                +'&taskId='+taskId
                +'&day='+day
                +'&finishedCard='+finishedCard
            )
            break;

        case 'del_task':
             var taskId = opts.taskId;
             var day = this.getCurrentDateFormat;
             //var day = '2018-01-02'
             return(
                'message-type=del_task'+'&username='+username
                +'&taskId='+taskId
                +'&day='+day
            )
            break;
        
        case 'query_index':
        var day = this.getCurrentDateFormat();
        //var day = '2018-01-02'
             return(
                'message-type=query_index'+'&username='+username+'&day='+day
            )
            break;
        
        case 'query_time_shaft':
             return(
                    'message-type=query_time_shaft'+'&username='+username
                )
            break;
        
        case 'query_moment':
            return(
                    'message-type=query_moment'+'&username='+username
                )
            break;
        case 'new_goal':
        messageType = 'new_goal';
        subTaskList = opts.subTaskList;//{'step': 1, 'days': 720, 'taskTitle': '存首期', 'taskContent': '存5000元', \
                              //'frequency': 30, 'beginDate': '2018-01-01', 'endDate': '2018-12-31'}
        username = opts.username;
        goalTitle = opts.goalTitle;
        goalType = opts.goalType;
        days = opts.days;
        beginDate = opts.beginDate;
        endDate = opts.endDate;
        step = JSON.stringify(subTaskList);
        return(
                    'message-type=new_goal'+'&username='+username
                    +'&goalTitle='+goalTitle+'&goalType='+goalType
                    +'&days='+days+'&beginDate='+beginDate
                    +'&endDate='+endDate+'&step='+step
                )
            break;
        }
        /*if (service == 'request_uploadSmall')
        {
            return ({
                             'Content-Type': 'application/json;charset=UTF-8',
                             'message-type':service,
                             'goalSize':'small',
                             'uid':uid,
                        })
        }
        else if (service == 'request_uploadBig')
        {
            return ({
                             'Content-Type': 'application/json;charset=UTF-8',
                             'message-type':service,
                             'goalSize':'big',
                             'uid':uid,
                        })
        }*/

    }

	/**
     * 普通的get请求 
     * @param {*} url 地址
     * @param {*} opts  参数{method:'GET',body:params}
     * @param {*} callback  成功后的回调
     
    static get(url,opts,callback){
       fetch(url,opts)
        .then((response) => {
            if(response.ok){//如果相应码为200
                return response.json(); //将字符串转换为json对象
            }
        })
        .then((jsonData) => {
                callback(jsonData);
        }).catch((error) =>{
	//网络请求失败
			alert("network fail");
		});
    };
*/

    /**
     * post key-value 形式 header为'Content-Type': 'application/x-www-form-urlencoded'
     * @param {*} url 
     * @param {*} service对应的请求页面
     * @param {*} opts对应的选项
     * @param {*} callback 
     */
    static post(url,service,opts,callback){
        var headers = new Headers({
                'Content-Type': 'application/x-www-form-urlencoded',
        });
    	//var headers = this.genHeaders(service,opts);
        //headers.append('message-type','new_day');
        //headers.append('username','test');
        //headers.append('Content-Type','application/x-www-form-urlencoded');
        var formData = this.genData(service,opts);
        console.log('post request');
        console.log(url);
        fetch(url,{
        	method:'POST',
        	headers:headers,
            body: formData,
        })
        .then((response) => {
            console.log(response);
            if(response.ok){
                console.log('response ok');
               //console.log(JSON.parse("{'a':1}"));
                //console.log(response._bodyText);
               return (JSON.parse(response._bodyText));
           }
        })
        .then((json) => {
        		//console.log(json);
                callback(json);
        }).catch((error) => {
            console.log(error);
        });
    };

    /**
     * post json形式  header为'Content-Type': 'application/json'
     * @param {*} url 
     * @param {*} opts {method:'POST',headers:{'Content-Type': 'application/json;charset=UTF-8'},body:JSON.stringify(jsonObj),//json对象转换为string}
     * @param {*} jsonObj 
     * @param {*} callback 
     */
    static postJson(url,service,opts,jsonObj,callback){
    	var headers = this.genHeaders(service,opts);
    	
        fetch(url,{
        	method:'POST',
        	headers:headers,
        	body:JSON.stringify(jsonObj),
        })
        .then((response) => {
            if(response.ok){
                return response.json();
            }
        })
        .then((json) => {
                console.log(json);
        }).catch((error) => {
        	//console.log(json);
            console.log(error);
        });
    };

    /**
     * 获取当前系统时间 yyyyMMddHH
     */
    static getCurrentDateFormat(){
        var space = "-";
        var dates = new Date();
        var years = dates.getFullYear();
        var months = dates.getMonth()+1;
        if(months<10){
            months = "0"+months;
        }

        var days = dates.getDate();
        if(days<10){
            days = "0"+days;
        }
        var time = years+space+months+space+days;
        return time;
    };

}